// pages/order/order.js
const app = getApp();
var baseurl = app.baseurl.url;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    winWidth: 0,
    winHeight: 0,
    currentTab: 0,
    reservStatus: [],
    title: '',
    order: [],  // 列表
    total: '', // 总共条数
    per_page: 4, // 每页条数
    last_page: 1, // 最后一页
    current_page: 1,// 当前页
    isloading: false
  },

  // 触底加载
  getMore1(that) {
    that.setData({
      isloading: true
    })
    var page = that.data.current_page;//页码
    page++;
    console.log("1111111111",page)
    console.log("1111111111",that.data.current_page)
    console.log("222222222222",that.data.last_page)
    // if (page == 1) {
    //   return true
    // } else {
    // if (res.data.data.last_page == page) {
    if (that.data.last_page == page) {
      wx.showToast({
        title: '没有更多数据！',
        icon: 'none',
        duration: 2000
      })
      // }
    }
    // 需求列表
    wx.request({
      url: `${baseurl}api/Reservation/reservRecord`,
      data: {
        page: page,
        openid: wx.getStorageSync("openid"),
        id: wx.getStorageSync("id"),
        title: this.data.title
      },
      success: function (res) {
        // console.log(res.data.data);
        that.setData({
          order: [...that.data.order, ...res.data.data.data],
          current_page: res.data.data.current_page,
          last_page: res.data.data.last_page,
          per_page: res.data.data.per_page,
          total: res.data.data.total
        })



      }

    });
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    /**
     * 获取当前设备的宽高
     */
    wx.getSystemInfo({

      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }

    });
    // 状态
    wx.request({
      url: `${baseurl}api/Reservation/reservStatus`,
      method: 'POST',

      success(res) {
        // console.log("11111",res.data.data);
        that.setData({
          reservStatus: res.data.data
        })
      }
    })


    // 列表
    // order
    wx.request({
      url: `${baseurl}api/Reservation/reservRecord`,
      data: {
        page: that.data.current_page,
        openid: wx.getStorageSync("openid"),
        id: wx.getStorageSync("id"),
        title: this.data.title
      },
      success: function (res) {
        console.log("11111",res);
        that.setData({
          order: [...that.data.order, ...res.data.data.data],
          current_page: res.data.data.current_page,
          last_page: res.data.data.last_page,
          per_page: res.data.data.per_page,
          total: res.data.data.total
        })

      }
    })

    let openid = wx.getStorageSync("openid")
    // console.log(wx.getStorageSync("openid"));
    // 是否有openid登录
    if (openid != '') {
      return true
      // console.log(111);
    } else {
      wx.reLaunch({
        url: '/pages/login/login'
      })
      // console.log(222);
    }

  },
  //  tab切换逻辑
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },

  // islogin() {
  //   let openid = wx.getStorageSync("openid")
  //   // console.log(wx.getStorageSync("openid"));
  //   // 是否有openid登录
  //   if (openid != '') {
  //     return true
  //     // console.log(111);
  //   } else {
  //     wx.navigateTo({
  //       url: '/pages/login/login'
  //     })
  //     // console.log(222);
  //   }
  // },


  // 搜索
  iptchange(e) {
    this.setData({
      title: e.detail.value
    })
    let that = this
    wx.request({
      url: `${baseurl}api/Reservation/reservRecord`,
      method: 'POST',
      data: {
        openid: wx.getStorageSync("openid"),
        id: wx.getStorageSync("id"),
        title: that.data.title
      },
      success(res) {
        // console.log(res.data.data);
        that.setData({
          order: res.data.data
        })
      }
    })

  },

  bindChange: function (e) {

    var that = this;
    that.setData({
      currentTab: e.detail.current
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.onLoad()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    this.getMore1(that)
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})